/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.entity.ClinicDeathProve;
import com.jims.master.modules.doctor.service.ClinicDeathProveService;

/**
 * Controller
 * @author 系统管理员
 * @version 2017-07-16
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/clinicDeathProve")
public class ClinicDeathProveController extends BaseController {

	@Autowired
	private ClinicDeathProveService clinicDeathProveService;
	@Autowired
	PatMasterIndexService patMasterIndexService;
	@Autowired
	ClinicMasterService clinicMasterService;
	
	@ModelAttribute
	public ClinicDeathProve get(@RequestParam(required=false) String id) {
		ClinicDeathProve entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clinicDeathProveService.get(id);
		}
		if (entity == null){
			entity = new ClinicDeathProve();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(String clinicId, String patientId, HttpServletRequest request, HttpServletResponse response, Model model) {
		ClinicDeathProve clinicDeathProve=clinicDeathProveService.findByConditions(clinicId, patientId);
		PatMasterIndex patMasterIndex=patMasterIndexService.get(patientId);
		ClinicMaster clinicMaster=clinicMasterService.get(clinicId);
		if(clinicDeathProve==null){
			String date ="";
			date= DateUtils.getDateTime();
			model.addAttribute("writeDate", DateUtils.parseDate(date));
		}else{
			model.addAttribute("writeDate", clinicDeathProve.getWriteDate());
		}
		model.addAttribute("clinicDeathProve", clinicDeathProve);
		model.addAttribute("patMasterIndex", patMasterIndex);
		model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/clinicDeathProveIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ClinicDeathProve clinicDeathProve, Model model) {
		model.addAttribute("clinicDeathProve", clinicDeathProve);
		return "modules/doctor/clinicDeathProveForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ClinicDeathProve clinicDeathProve, Model model) {
	    StringData data=new StringData();
		int i=clinicDeathProveService.save(clinicDeathProve);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ClinicDeathProve clinicDeathProve) {
	    StringData data=new StringData();
		int i=clinicDeathProveService.delete(clinicDeathProve);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
	/**
	 * 死亡证明打印
	 * @param clinicId
	 * @param patientId
	 * @return
	 * @author fd
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"clinicDeathProvePrint", ""})
	@ResponseBody
	public StringData clinicDeathProvePrint(String clinicId,String patientId){
		StringData stringData = new StringData();
		String printPath =clinicDeathProveService.clinicDeathProvePrint(clinicId,patientId);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}
}